<%--
  Created by IntelliJ IDEA.
  User: 494418174
  Date: 2020/3/19 0019
  Time: 9:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户名" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">起始积分:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="minPoint" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入起始积分" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束积分:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="maxPoint" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入结束积分" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">性别:</label>
            <div class="layui-input-inline">
                <input type="radio" name="sex"  value="1"  title="男">
                <input type="radio" name="sex"  value="0" title="女">
                <input type="radio" name="sex"   value="" title="全部" checked="checked">
            </div>
        </div>
        <div class="layui-input-black" style="text-align: center">
            <button type="button" class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm " lay-submit lay-filter="formDemo">查询
            </button>
            <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->

<table id="userTable" lay-filter="userTable"></table>

<script id="toolbarUser" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="userAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="userDelChk">批量删除</button>
    </div>
</script>

<script id="barUser" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="resetPwd">重置密码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs " lay-event="del">删除</a>
</script>

<%-- 添加或者修改弹出框开始 --%>
<div style="display: none" id="addOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">登录账号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="hidden" name="userId">
                <input type="text" name="loginId" id="loginId" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录账号" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
            <div class="layui-inline" id="d1">
            <label class="layui-form-label">登录密码:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="loginPwd" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入登录密码" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
            </div>
            <div class="layui-inline" id="d2">
            <label class="layui-form-label">用户积分:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="number" name="point" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入用户积分" style="height: 30px;border-radius: 10px" >
            </div>
             </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="phone" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入手机号" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
            <label class="layui-form-label">姓名:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="userName" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入真实姓名" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" checked title="男">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
            <label class="layui-form-label">地区:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="address" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入地区" style="height: 30px;border-radius: 10px" lay-verify="required">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button"
                        class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                        lay-filter="doSubmit" lay-submit="">提交
                </button>
                <button type="reset"
                        class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">重置
                </button>
            </div>
        </div>
    </form>
</div>
<%-- 添加或者修改弹出框结束 --%>

<script src="/layui/layui.js"></script>
<script>
    var userTable
    var index
    var url
    layui.use(['table','form','layer','jquery'], function() {
        var table = layui.table
        var form = layui.form
        var layer = layui.layer
        var $ = layui.jquery

        // 初始table
        userTable = table.render({
            elem: "#userTable"
            , url: "/adminUser/findUser"
            , page: {limit: 5, limits: [5, 10, 15]}
            , cellMinWidth: 80
            , toolbar: '#toolbarUser'
            , cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'userId', title: '用户ID', fixed: 'center'}
                , {field: 'loginId', title: '登录账号', fixed: 'center'}
                , {field: 'userName', title: '用户姓名', fixed: 'center'}
                , {field: 'sex', title: '性别', fixed: 'center' ,templet: function (data) {
                        return data.sex == 1 ? "男" : "女"
                    }}
                , {field: 'address', title: '家庭地址', fixed: 'center'}
                , {field: 'phone', title: '联系方式', fixed: 'center'}
                , {field: 'point', title: '积分', fixed: 'center'}
                , {field: 'createTime', title: '注册时间', fixed: 'center'}
                , {fixed: 'right', title: '操作', toolbar: '#barUser', width: 250}
            ]]
        })


        // 查询
        form.on("submit(formDemo)", function (data) {
            userTable.reload({
                url: "/adminUser/findUser"
                , where: {
                    'sex': data.field.sex
                    ,'minPoint':data.field.minPoint
                    ,'userName':data.field.userName
                    ,'maxPoint':data.field.maxPoint

                }
                , page: {
                    curr: 1
                }
            })
            return false;
        })
        // 头部工具栏添加事件
        table.on('toolbar(userTable)',function(obj) {
            if(obj.event == "userAdd"){ // 点击新增按钮执行的内容
                userAdd()
            }else if(obj.event=="userDelChk"){
                userDelChk(obj)
            }
        })
        //监听行工具事件
        table.on('tool(userTable)', function(obj){
            var data = obj.data
            if(obj.event == "edit"){
                menuUpdate(data)
            }else  if(obj.event == "del"){
                del(data)
            }else if(obj.event=="resetPwd"){
                resetPwd(data)
            }
        })
        //重置密码
        function resetPwd(data) {
            layer.confirm("是否重置密码为123456",function (ind) {
                $.post("/adminUser/resetUserPwd",{"userId":data.userId},function (r) {
                    layer.msg(r.msg)
                    userTable.reload()
                })
                layer.close(ind)
            })
        }


        function userDelChk(obj) {
            var checkStatus=table.checkStatus(obj.config.id);
            var data=checkStatus.data
            if(data.length==0){
                layer.msg("请至少选择一个数据")
            }else {
                var userIds=new Array()
                $.each(data,function (index,item) {
                    userIds.push(item.userId)
                })
                layer.confirm("你确定要删除"+data.length+"条数据么",function (ind) {
                    $.post("/adminUser/userDelChk",{"ids":userIds},function (r) {
                        layer.msg(r.msg)
                        userTable.reload()
                    })
                    layer.close(ind)
                })
            }
        }
        function del(data){


            layer.confirm("您确定要删除"+ data.userName +"用户么",function(ind){

                $.post("/adminUser/delUser", {"userId":data.userId} ,function(d) {
                    layer.msg(d.msg)
                    // 刷新table数据
                    userTable.reload()

                })
                layer.close(ind)
            })


        }
        function menuUpdate(data) {
            index =  layer.open({
                type:1
                , title:"修改用户"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    form.val("dataFrm",data)
                    $("#d1").hide()
                    $("#d2").show()
                    $("#loginId").addClass("layui-btn-disabled")
                    url="/adminUser/updateUser"
                }
            })
        }
        function userAdd() {
            // 弹出框
            index =  layer.open({
                type:1 //
                , title:"添加用户"
                , content:$("#addOrUpdateDiv")
                , area : ['800px', '440px']
                , success:function(layero, index) {
                    // 页面数据要清除
                    $("#dataFrm")[0].reset()
                    $("#d1").show()
                    $("#d2").hide()
                    $("#loginId").removeClass("layui-btn-disabled")
                    url="/adminUser/addUser"
                }
            })

        }
        // form表达提交
        form.on("submit(doSubmit)",function() {
            layer.confirm("您确定要提交该数据么",function(ind){
                $.post(url,$("#dataFrm").serialize(),function(data) {
                    layer.msg(data.msg)
                    layer.close(index)
                    userTable.reload()
                })
                layer.close(ind)
            })





        })
    })

</script>
</body>
</html>
